package com.kakaxi.mybilibili.module.video.view;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.github.czy1121.view.RoundButton;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.base.BaseFragment;
import com.kakaxi.mybilibili.bean.EventBusObject;
import com.kakaxi.mybilibili.bean.VideoIntroBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 视频简介碎片类
 * Created by kakaxi on 16/11/20.
 */

public class VideoIntroFragment extends BaseFragment {

    /** 标题 **/
    @BindView(R.id.txt_video_intro_title)
    TextView mTitleTxt;
    /** 详情 **/
    @BindView(R.id.txt_video_intro_desc)
    TextView mDescTxt;

    /** 播放量 **/
    @BindView(R.id.txt_video_intro_play)
    TextView mPlayTxt;
    /** 弹幕量 **/
    @BindView(R.id.txt_video_intro_danmaku)
    TextView mDanmakuTxt;

    /** up主名 **/
    @BindView(R.id.txt_video_intro_up_name)
    TextView mUpNameTxt;
    /** 发布时间 **/
    @BindView(R.id.txt_video_intro_up_publish)
    TextView mUpPublishTxt;
    /** up主头像 **/
    @BindView(R.id.civ_video_intro_up_face)
    CircleImageView mUpFaceCIV;

    /** 分享正文 **/
    @BindView(R.id.llayout_video_intro_action_share_content)
    LinearLayout mShareActionContentLLayout;
    /** 分享数量 **/
    @BindView(R.id.roubtn_video_intro_action_share_num)
    RoundButton mShareActionNumBtn;

    /** 硬币内容 **/
    @BindView(R.id.llayout_video_intro_action_coin_content)
    LinearLayout mCoinActionContentLLayout;
    /** 硬币数量 **/
    @BindView(R.id.roubtn_video_intro_action_coin_num)
    RoundButton mCoinActionNumBtn;

    /** 收藏内容 **/
    @BindView(R.id.llayout_video_intro_action_collect_content)
    LinearLayout mCollectActionContentLLayout;
    /** 收藏数量 **/
    @BindView(R.id.roubtn_video_intro_action_collect_num)
    RoundButton mCollectActionNumBtn;

    /** 缓存 **/
    @BindView(R.id.llayout_video_intro_action_download_content)
    LinearLayout mDownloadActionContentLLayout;
    /** 缓存数量 **/
    @BindView(R.id.roubtn_video_intro_action_download_num)
    RoundButton mDownloadActionNumBtn;

    /** 关注按钮 **/
    @BindView(R.id.roubtn_video_intro_consider)
    RoundButton mConsiderBtn;


    public static VideoIntroFragment newInstance() {
        VideoIntroFragment videoIntroFragment = new VideoIntroFragment();
        return videoIntroFragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_video_intro;
    }

    @Override
    protected void setupViews(ViewGroup viewGroup) {
        ButterKnife.bind(this, viewGroup);
    }

    @Override
    protected void initData() {
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 巴士事件传递方法, 为控件设置视频介绍的数据
     *
     * @param object 视频介绍类
     */
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onVideoInfoEventBus(EventBusObject object) {
        if (object.getEvent() == EventBusObject.EVENT_VIDEO_INTRO_FRAG_SHOW_INFO) {
            VideoIntroBean videoIntroBean = (VideoIntroBean) object.getData();
            // 设置介绍信息
            mTitleTxt.setText(videoIntroBean.getTitle());
            mDescTxt.setText(videoIntroBean.getDescription());
            mPlayTxt.setText(videoIntroBean.getPlay());
            mDanmakuTxt.setText(videoIntroBean.getVideoReview());

            // 设置操作按钮信息
            setActionNumText(mShareActionNumBtn, videoIntroBean.getReview() + "", View.VISIBLE);
            setActionNumText(mCoinActionNumBtn, videoIntroBean.getCoins() + "", View.VISIBLE);
            setActionNumText(mCollectActionNumBtn, videoIntroBean.getFavorites() + "", View
                    .VISIBLE);
            setActionNumText(mDownloadActionNumBtn, "", View.INVISIBLE);

            // 设置up主信息
            Glide.with(getContext()).load(videoIntroBean.getFace()).crossFade().into(mUpFaceCIV);
            mUpNameTxt.setText(videoIntroBean.getAuthor());
            mUpPublishTxt.setText(videoIntroBean.getCreatedAt());

            // 显示关注按钮
            mConsiderBtn.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置操作按钮的数字文本
     *
     * @param btn        控件
     * @param text       数字文本
     * @param visibility 显示
     */
    private void setActionNumText(RoundButton btn, String text, int visibility) {
        btn.setText(text);
        btn.setVisibility(visibility);
    }

    @Override
    public String getTitle() {
        return "haha";
    }
}
